﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for DeliveryNote.xaml
    /// </summary>
    public partial class DeliveryNote : Window
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        DeliveryNoteData m_DeliveryNoteData;
        public DataSet setUI = new DataSet();
        public DataRow dwVKPOS;
        public DataRow dwVVLIEFSCH;
        public DataRow dwCOLOR;
        public DataRow dwWBBST;
        // string OrderNumber="";
        
        #endregion
        public DeliveryNote()
        {
            InitializeComponent();
        }
        public DeliveryNote(Dictionary<string, object> dicParamater)
        {
            InitializeComponent();
            this.dicParamater = dicParamater;
            this.txt1.Text = "任务书编号";//"07"
            /*this.txtArticleVar.Text = "A1";//"000012";
            this.txtColor.Text = "RD01      ";
            this.txtFinish.Text = "F01";*/

        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {

                //dsParamaters = ((IntexReportClient.DataSetIntex)(this.FindResource("dataSetIntex")));
                InitDataBinding();
                orlReadCommand = OracleDB.CreateCommand();

                orlReadCommand.CommandText = "SELECT B.KDKUNDNA,A.VKPAUFKRS,A.VKPAUFLFD,A.VKPAUFPOS,A.VKPART,"
                                          + " A.VKPFARBE,A.VKPGROE,A.VKPMENGE1,A.VKPMEEH,A.VKPKDKOMM,A.VKPZUSATZ,"
                                          + " trim(A.VKPKDBEZ)||trim(A.VKPKDARTNR)||trim(VKPKDFARBNR) as STR"
                                          + " FROM INTEX.VKPOS A,INTEX.KDSTAMM B  WHERE A.VKPMANDANT = B.KDMANDANT "
                                          + " AND A.VKPKUNDNR = B.KDKUNDNR AND VKPMANDANT = '00' "
                                          + " AND TRIM(VKPSA)IS NULL AND VKPAUFART ='NA' AND VKPERL <>'J' ";


                dsResult = m_DeliveryNoteData.Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "SALESORDER";
                    Queue Q100 = new Queue();
                    Q100.Enqueue("VKPMENGE1");
                    m_DeliveryNoteData.DivideBy100(dsResult.Tables[0], Q100);

                    byte[] b;
                    string value;
                    foreach (DataRow dw in dsResult.Tables[0].Rows)
                    {

                        foreach (DataColumn dc in dsResult.Tables[0].Columns)
                        {
                            if (dc.ColumnName == "KDKUNDNA")
                            {
                                b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[dc.ColumnName].ToString().Trim());
                                value = Encoding.GetEncoding("gb2312").GetString(b);
                                dw[dc.ColumnName] = value;
                            }
                            if (dc.ColumnName == "STR")
                            {
                                b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[dc.ColumnName].ToString().Trim());
                                value = Encoding.GetEncoding("gb2312").GetString(b);
                                dw[dc.ColumnName] = value;
                            }
                            if (dc.ColumnName == "VKPFARBE")
                            {
                                b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[dc.ColumnName].ToString().Trim());
                                value = Encoding.GetEncoding("gb2312").GetString(b);
                                dw[dc.ColumnName] = value;
                            }

                        }
                    }
                    foreach (DataTable dtTemp in dsResult.Tables)
                    {
                        setUI = m_DeliveryNoteData.ImportDataTable(setUI, dtTemp);
                    }
                }
                setUI.AcceptChanges();
                this.DataContext = setUI;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
            return;

        }
        private void InitDataBinding()
        {

            m_DeliveryNoteData = new DeliveryNoteData();
            m_DeliveryNoteData.OracleDB = this.OracleDB;
            m_DeliveryNoteData.proxy = this.proxy;

            this.setUI.Tables.Add("VKPOS");
            this.setUI.Tables.Add("VVTLIEF");
            this.setUI.Tables.Add("COLOR");
            this.setUI.Tables.Add("WBBST");
            orlReadCommand = OracleDB.CreateCommand();

            #region "VKPOS"
            orlReadCommand.CommandText = "SELECT B.KDKUNDNA,A.VKPAUFKRS,A.VKPAUFLFD,A.VKPAUFPOS,A.VKPART,"
                                                  + " A.VKPFARBE,A.VKPGROE,round(A.VKPMENGE1/100,3) AS QUANTITY,A.VKPMEEH,"
                                                  + " A.VKPKDBEZ,A.VKPKDARTNR,A.VKPKDKOMM,A.VKPZUSATZ"
                                                + " FROM VKPOS A,KDSTAMM B  WHERE A.VKPMANDANT = B.KDMANDANT "
                                                + " AND A.VKPKUNDNR = B.KDKUNDNR AND RowNum<1";

            dsResult = m_DeliveryNoteData.Connect2Proxy(orlReadCommand);

            if (dsResult.Tables.Count > 0)
            {
                dsResult.Tables[0].TableName = "VKPOS";
                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    setUI = m_DeliveryNoteData.ImportDataTable(setUI, dtTemp);
                }
            }
            #endregion

            #region "VVTLIEF"
            orlReadCommand.CommandText = "SELECT VVTLIEFSCH,VVTLIEFSCHPOS,VVSTATUS,VVVERSART,VVSPED,VVTRENR,"
                                        + " VVAVISNR,VVVERSDAT,round(VVTMTR/100/0.9144,3) AS QUANTITY,VVTKG/1000 AS KG,VVTAUFKRS,VVTAUFLFD,VVTAUFPOS "
                                        + " FROM VVTLIEF A,VVLIEF B WHERE A.VVTMANDANT = B.VVMANDANT "
                                        + " AND A.VVTLIEFSCH = B.VVLIEFSCH AND vvtmandant = '00' AND RowNum<1";
            //decode(VVSTATUS,'LI','信用限制','KO','发货单不完整','EN','提货单未打印','VF','未发货','VS','已出货未产生发票','IK','数据未完全确认','SP','锁定','VR','已产生发票',VVSTATUS) AS STATUS,
            dsResult = m_DeliveryNoteData.Connect2Proxy(orlReadCommand);

            if (dsResult.Tables.Count > 0)
            {
                dsResult.Tables[0].TableName = "VVTLIEF";

                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    this.setUI = m_DeliveryNoteData.ImportDataTable(setUI, dtTemp);
                }

            }
            #endregion

            #region "COLOR"
            orlReadCommand.CommandText = "select substr(sdkey,3,3) as COLOR,substr(sddaten,2,10) as DES "
                                        + " from sddat "
                                        + " where sdsa = '0201' and substr(sdkey,1,2) = '00' AND substr(sdkey,24,1)= 'V'AND RowNum<1";

            dsResult = m_DeliveryNoteData.Connect2Proxy(orlReadCommand);

            if (dsResult.Tables.Count > 0)
            {
                dsResult.Tables[0].TableName = "COLOR";

                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    this.setUI = m_DeliveryNoteData.ImportDataTable(setUI, dtTemp);
                }

            }
            #endregion

            #region "WBBST"
            orlReadCommand.CommandText = "SELECT rownum AS CODE,WBBRWLFD AS CARD,WBBPOS AS ITM,WBBSTKNR AS PIECENO,"
                                        + " WBBART AS ARTICLE,WBBVAR AS VAR,WBBFARBE AS COLOR,WBBCOLORIT AS COLORIT,"
                                        + " ROUND(WBBISTKLG/1000/0.9144,1) AS ACTYAND,ROUND(WBBIGEW/1000,1) AS ACTWEI,WBBURLIEFSCH,"
                                        + " ROUND(WBBIBRT/1000,0) AS WIDTH,WBBDPART AS LOT,WBBZENSUR||WBBZENSURKZ AS QUALITY,"
                                        + " WBBAUFKRS,WBBAUFLFD,WBBAUFPOS,WBBALIEFSCH,WBBTAX "
                                        + " FROM WBBST WHERE WBBMANDANT = '00' AND RowNum<1 ";

            dsResult = m_DeliveryNoteData.Connect2Proxy(orlReadCommand);

            if (dsResult.Tables.Count > 0)
            {
                dsResult.Tables[0].TableName = "WBBST";

                foreach (DataTable dtTemp in dsResult.Tables)
                {

                    setUI = m_DeliveryNoteData.ImportDataTable(setUI, dtTemp);
                }
            }
            #endregion


            setUI.AcceptChanges();
            this.VKPOSDataGrid.DataContext = setUI;
        }
        private void MainDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (DataTable dtTemp in setUI.Tables)
            {
                if (dtTemp.TableName != "SALESORDER")
                {
                    dtTemp.Clear();
                }
            }
            setUI.AcceptChanges();
            try
            {
                if (e.AddedItems.Count == 0)
                    return;

                DataRowView drv = e.AddedItems[0] as DataRowView;
                dwVKPOS = drv.Row;
                this.txtVKPAUFLFD.Text = dwVKPOS["VKPAUFKRS"].ToString() + dwVKPOS["VKPAUFLFD"].ToString();


                ReloadVVTLIEF(dwVKPOS);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void listVVTLIEF_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (e.AddedItems.Count == 0)
                    return;
                DataRowView drv = e.AddedItems[0] as DataRowView;
                dwVVLIEFSCH = drv.Row;
                this.txtVVLIEFSCH.Text = dwVVLIEFSCH["VVTLIEFSCH"].ToString();


                ReloadCOLOR(dwVVLIEFSCH);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
        }
        private void listCOLOR_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (e.AddedItems.Count == 0)
                    return;
                DataRowView drv = e.AddedItems[0] as DataRowView;
                dwCOLOR = drv.Row;
                this.txtCOLOR.Text = dwCOLOR["COLOR"].ToString();

                dwVVLIEFSCH["WBBTAX"] = dwCOLOR["COLORSD"].ToString();
                ReloadWBBST(dwVVLIEFSCH);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }

        }
        private void ReloadVVTLIEF(DataRow dwVKPOS)
        {
            dwWBBST = setUI.Tables["WBBST"].Clone().Rows.Add();
            dwWBBST["WBBAUFKRS"] = dwVKPOS["VKPAUFKRS"];
            dwWBBST["WBBAUFLFD"] = dwVKPOS["VKPAUFLFD"];
            dwWBBST["WBBAUFPOS"] = dwVKPOS["VKPAUFPOS"];

            setUI = m_DeliveryNoteData.GetVVTLIEF(setUI, dwWBBST);
            this.listVVTLIEF.DataContext = setUI;
        }

        private void ReloadCOLOR(DataRow dwVVLIEFSCH)
        {
            dwWBBST = setUI.Tables["WBBST"].Clone().Rows.Add();
            dwWBBST["WBBAUFKRS"] = dwVVLIEFSCH["VVTAUFKRS"];
            dwWBBST["WBBAUFLFD"] = dwVVLIEFSCH["VVTAUFLFD"];
            dwWBBST["WBBAUFPOS"] = dwVVLIEFSCH["VVTAUFPOS"];
            dwWBBST["WBBALIEFSCH"] = dwVVLIEFSCH["VVTLIEFSCH"];

            setUI = m_DeliveryNoteData.GetCOLOR(setUI, dwWBBST);
            this.listCOLOR.DataContext = setUI;
        }
        private void ReloadWBBST(DataRow dwVVLIEFSCH)
        {
            dwWBBST = setUI.Tables["WBBST"].Clone().Rows.Add();
            dwWBBST["WBBAUFKRS"] = dwVVLIEFSCH["VVTAUFKRS"];
            dwWBBST["WBBAUFLFD"] = dwVVLIEFSCH["VVTAUFLFD"];
            dwWBBST["WBBAUFPOS"] = dwVVLIEFSCH["VVTAUFPOS"];
            dwWBBST["WBBALIEFSCH"] = dwVVLIEFSCH["VVTLIEFSCH"];
            dwWBBST["WBBTAX"] = dwVVLIEFSCH["WBBTAX"];
            setUI = m_DeliveryNoteData.GetWBBST(setUI, dwWBBST);
            this.listWBBST.DataContext = setUI;
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            DataRow dwTmp ;
            DataSet setRpt = new DataSet();
            setRpt = m_DeliveryNoteData.BuildDeliveryNoteData(setRpt, dwWBBST);
            if (setRpt.Tables["WBBST"].Rows.Count > 0)
            {
                dwTmp = setRpt.Tables["WBBST"].Rows[0];
                dicParamater["WBBAUFKRS"] = dwTmp["WBBAUFKRS"].ToString();
                dicParamater["WBBAUFLFD"] = dwTmp["WBBAUFLFD"].ToString();
                dicParamater["WBBAUFPOS"] = dwTmp["WBBAUFPOS"].ToString();
                dicParamater["WBBALIEFSCH"] = dwTmp["WBBALIEFSCH"].ToString();
                dicParamater["WBBTAX"] = dwTmp["WBBTAX"].ToString();

                DeliveryNoteReport m_rpt = new DeliveryNoteReport(dicParamater);
                m_rpt.dsSource = setRpt;
                m_rpt.GetDeliveryNoteReport("EXCEL");
            }
        }

    }
}
